import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/entities/print_detail_entity.dart';
import 'package:library_base/entities/print_not_single_entry_details_entity.dart';
import 'package:library_base/entities/print_single_entry_details_entity.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:qr_flutter/qr_flutter.dart';

Widget buildItem(
  PrintDetailEntity model,
    int index,
    GlobalKey globalKey, {
  Callback<PrintDetailEntity>? onTap,
  Function(PrintDetailEntity,int,GlobalKey)? onPrintSingle,
}) {
  List<Widget> widgets = [];

  widgets.add(gapH60);

  widgets.add(Stack(
    children: [
      Container(
        alignment: Alignment.center,
        width: double.infinity,
        padding: EdgeInsets.symmetric(vertical: 40.w),
        child: Text(
          MultiLanguage.customerDeliveryNote.tr,
          style: normalBoldTextColor50,
        ),
      ),
      Positioned(
          top: 45.w,
          right: 24.w,
          child: Text(model.carNo, style: normalTextColor28))
    ],
  ));

  widgets.add(line4);
  widgets.add(gapH18);
  widgets.add(Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("发货单位: ", style: normalTextColor36),
        Expanded(child: Text(model.shipperName, style: normalTextColor36))
      ],
    ),
  ));
  widgets.add(gapH18);
  widgets.add(Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("销售单号: ", style: normalTextColor36),
        Expanded(child: Text(model.orderNumber, style: normalTextColor36))
      ],
    ),
  ));
  widgets.add(gapH18);
  widgets.add(Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("收货人: ", style: normalTextColor36),
        Expanded(child: Text(model.consignee, style: normalTextColor36))
      ],
    ),
  ));
  widgets.add(gapH18);
  widgets.add(Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("收货人电话: ", style: normalTextColor36),
        Expanded(
            child: Text(model.mobilePhone, style: normalTextColor36))
      ],
    ),
  ));
  widgets.add(gapH18);
  widgets.add( Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("收货地址: ", style: normalTextColor36),
        Expanded(child: Text(model.address, style: normalTextColor36With9Spacing))
      ],
    ),
  ),);
  widgets.add(gapH18);
  widgets.add( Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("配送时间: ", style: normalTextColor36),
        Expanded(
            child: Text(model.stationTime, style: normalTextColor36))
      ],
    ),
  ),);
  widgets.add(gapH18);
  widgets.add(line4);
  widgets.add(gapH60);

  if(model.singleEntryDetails.isNotEmpty){
    widgets.add(buildSingleEntryDetails(model.singleEntryDetails));
  }
  if(model.notSingleEntryDetails.isNotEmpty){
    if(model.singleEntryDetails.isNotEmpty){
      widgets.add(gapH60);
    }
    widgets.add(buildNotSingleEntryDetails(model.notSingleEntryDetails));
  }
  if(model.singleEntryDetails.isNotEmpty){
    String singleCodeAll = "";
    for(int i = 0; i< model.singleEntryDetails.length;i++){
      if(model.singleEntryDetails[i].goodsBarCode.length>4){
        var singleCode = model.singleEntryDetails[i].goodsBarCode.substring(model.singleEntryDetails[i].goodsBarCode.length-4,model.singleEntryDetails[i].goodsBarCode.length);
        singleCodeAll = singleCodeAll+singleCode;
        if(i != (model.singleEntryDetails.length-1)){
          singleCodeAll = singleCodeAll+"  ";
        }
      }else{
        var singleCode = model.singleEntryDetails[i].goodsBarCode;
        singleCodeAll = singleCodeAll+singleCode;
        if(i != (model.singleEntryDetails.length-1)){
          singleCodeAll = singleCodeAll+"  ";
        }
      }
    }
    if(model.singleEntryDetails.isNotEmpty || model.notSingleEntryDetails.isNotEmpty ){
      widgets.add(gapH40);
    }
    widgets.add( Container(
      width: double.infinity,
      padding: edgeLR24,
      child: Text(singleCodeAll, style: normalTextColor36,textAlign: TextAlign.start,)
    ));
  }
  widgets.add(gapH60);

  widgets.add( Padding(
    padding: edgeLR24,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(model.shortAddress, style: normalTextColor50),
      ],
    ),
  ),);
  widgets.add(gapH18);

  widgets.add(QrImage(
    data:model.orderNumber,
    size: 300.w,
    embeddedImageStyle: QrEmbeddedImageStyle(size: Size(50.w, 50.w)),
  ));

  widgets.add(gapH18);

  return Container(
    color: Colors.white,
    padding: EdgeInsets.symmetric(horizontal: 24.w),
    child: Column(children: [
      RepaintBoundary(
        key: globalKey,
        child: Container(
          width: double.infinity,
          decoration:
              BoxDecoration(color: Colors.white,border: Border.all(color: Colors.black, width: 1)),
          child: Column(mainAxisSize: MainAxisSize.min, children: widgets),
        ),
      ),
      gapH36,
      GradientButton(
        ThemeUtil.getTheme(),
        width: double.infinity,
        padding: edgeAll24 as EdgeInsets,
        text: MultiLanguage.print.tr,
        onPressed: (){
          onPrintSingle?.call(model,index,globalKey);
        },
      ),
      gapH36,
    ]),
  );
}

Widget buildSingleEntryDetails(List<PrintSingleEntryDetailsEntity> singleEntryDetails){
  List<TableRow> widgets = [];
  widgets.add(TableRow(
    children: [
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          alignment: Alignment.center,
          child: Text("商品条码",style: normalTextColor36,textAlign: TextAlign.center))),
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          alignment: Alignment.center,child: Text("商品名称",style: normalTextColor36,textAlign: TextAlign.center))),
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          alignment: Alignment.center,child: Text("主重量\n${SPUtil.isJin.unitStrZhWithBracket()}",style: normalTextColor36,textAlign: TextAlign.center))),
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          alignment: Alignment.center,child: Text("副重量\n${SPUtil.isJin.unitStrZhWithBracket()}",style: normalTextColor36,textAlign: TextAlign.center))),
    ],
  ));
  for(int i = 0; i< singleEntryDetails.length;i++){
    widgets.add(TableRow(
      children: [
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(singleEntryDetails[i].goodsBarCode,style: normalTextColor36,textAlign: TextAlign.center))),
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(singleEntryDetails[i].goodsName,style: normalTextColor36,textAlign: TextAlign.center))),
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(singleEntryDetails[i].mainWeight.numAsUnitStr(),style: normalTextColor36,textAlign: TextAlign.center))),
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(singleEntryDetails[i].subWeight.numAsUnitStr(),style: normalTextColor36,textAlign: TextAlign.center))),
      ],
    ));
  }
  return Container(
    margin: EdgeInsets.symmetric(horizontal: 8.w),
    child: Table(
      border: TableBorder.all(
        color: Colors.black,
        width: 1,
        style: BorderStyle.solid,
      ),
      children: widgets,
    ),
  );
}

Widget buildNotSingleEntryDetails(List<PrintNotSingleEntryDetailsEntity> notSingleEntryDetails){
  List<TableRow> widgets = [];
  widgets.add(TableRow(
    children: [
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          child: Text("商品名称",style: normalTextColor36,textAlign: TextAlign.center))),
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          alignment: Alignment.center,child: Text("数量",style: normalTextColor36,textAlign: TextAlign.center))),
      TableCell(
          verticalAlignment: TableCellVerticalAlignment.middle,
          child: Container(
          padding: edgeV12,
          alignment: Alignment.center,child: Text("重量\n${SPUtil.isJin.unitStrZhWithBracket()}",style: normalTextColor36,textAlign: TextAlign.center))),
    ],
  ));
  for(int i = 0; i< notSingleEntryDetails.length;i++){
    widgets.add(TableRow(
      children: [
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(notSingleEntryDetails[i].goodsName,style: normalTextColor36,textAlign: TextAlign.center))),
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(notSingleEntryDetails[i].count.toString(),style: normalTextColor36,textAlign: TextAlign.center))),
        TableCell(
            verticalAlignment: TableCellVerticalAlignment.middle,
            child: Container(
            padding: edgeV12,
            alignment: Alignment.center,child: Text(notSingleEntryDetails[i].weight.numAsUnitStr(),style: normalTextColor36,textAlign: TextAlign.center))),
      ],
    ));
  }
  return Container(
    margin: EdgeInsets.symmetric(horizontal: 8.w),
    child: Table(
      border: TableBorder.all(
        color: Colors.black,
        width: 1,
        style: BorderStyle.solid,
      ),
      children: widgets,
    ),
  );
}



